home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / comm / tcp / PowerWEB.lha / PowerWEB / EditTextFile.rexx next >
Encoding:
OS/2 REXX Batch file  |  1998-03-16  |  3.2 KB  |  119 lines

  1. /* 
  2. */
  3.    
  4. parse arg fModifyMe sReplaceMe sWithMe
  5.  
  6. address COMMAND
  7.  
  8. sReplaceMe = ChangeToSpaces(sReplaceMe)
  9. sWithMe    = strip(ChangeToSpaces(sWithMe))
  10.  
  11. fBackupFile = fModifyMe || ".bak"
  12. say "File to be modified   = [" || fModifyMe || "]"
  13. say "Backup file           = [" || fBackupFile || "]"
  14. say "String to add/replace = [" || sReplaceMe || "]"
  15. say "add/Replace with      = [" || sWithMe || "]"
  16.  
  17. keylength = length(sReplaceMe)
  18.  
  19. /* First, make sure rexxsupport.library is available. */
  20. call addlib("rexxsupport.library", 0, -30, 0)
  21.  
  22. /* Delete any .bak file that now exists */
  23. call delete(fBackupFile)
  24.  
  25. /* Rename the original filename to filename.bak */
  26. if (rename(fModifyMe, fBackupFile) == 0) then do
  27.     say "Error:  Couldn't rename " || fModifyMe || " to " || fBackupFile
  28.     exit(30)
  29.     end
  30. BFileRenamed = 1
  31.     
  32. /* Default == haven't found the line we want to replace yet */
  33. BFoundOurLine = 0
  34.  
  35. /* Open the backup file for reading */
  36. if (open('oldfile',fBackupFile,'R') == 0) then do
  37.     say "Couldn't open backup file " || fBackupFile
  38.     call error
  39.     end
  40.     
  41. /* and the new file for writing */
  42. if (open('newfile',fModifyMe,'W') == 0) then do
  43.     say "Couldn't open output file " || fModifyMe
  44.     call error
  45.     end
  46.  
  47.  
  48. /* Scan the old file, writing the new one */
  49. do while (EOF('oldfile') == 0)
  50.  nextline = readln('oldfile')
  51.  sCheckPart = left(nextline,keylength)
  52.  
  53.  if ((BFoundOurLine == 0)&(sCheckPart = sReplaceMe)) then 
  54.  do
  55.      /* don't write out this line... write out our substitute instead! */
  56.      say "Found: [" || nextline || "] replacing with [" || sWithMe || "]"
  57.     call writeln('newfile',sWithMe)
  58.     BFoundOurLine = 1
  59.  end
  60.  else do
  61.     call writeln('newfile',nextline)
  62.     end
  63. end
  64.  
  65. /* If we never found our line to replace, then just add our
  66.    line at the end of the file. */
  67. if (BFoundOurLine == 0) then call writeln('newfile',sWithMe)
  68.  
  69. call close('oldfile')
  70. call close('newfile')
  71.  
  72. /* success! */
  73. successMessage = "File " || fModifyMe || " successfully modified (" || sReplaceMe || ") -> " || "(" || sWithMe || ")."
  74.  
  75. say successMessage
  76.  
  77. /* Show calling app that we succeeded */
  78. address COMMAND 'echo ' || successMessage || ' >t:edit_text_succeeded'
  79. exit(0)
  80.  
  81.  
  82. /* Changes all occurrences of the character '^' to spaces in the string. 
  83.    Needed to get around ARexx's lame argument parsing.   I think.
  84. */
  85. ChangeToSpaces: procedure
  86.     parse arg sOrig
  87.     
  88.     sNew = ""
  89.     do while (length(sOrig) > 0)
  90.         cChar = left(sOrig,1)
  91.         if (cChar == "^") then cChar = " "
  92.         sNew = sNew || cChar
  93.         sOrig = right(sOrig,length(sOrig)-1)
  94.         end
  95.     return sNew
  96.         
  97.         
  98. /* Replaces original file if an error occurs */        
  99. error:
  100.     say "An error occured!"
  101.     call close('oldfile')
  102.     call close('newfile')
  103.     if (BFileRenamed == 1) then do
  104.         say "Attempting to restore the original file " || fModifyMe
  105.  
  106.         /* Delete the incomplete file that now exists */
  107.         call rename(fModifyMe, fModifyMe || ".tobedeleted")
  108.  
  109.         /* Rename filename.bak back to filename */
  110.         if (rename(fBackupFile, fModifyMe) == 0) then do
  111.             call rename(fModifyMe || ".tobedeleted", fModifyMe)
  112.             say "Error:  Couldn't rename " || fBackupFile || " to " || fModifyMe || ", aborting now."
  113.             exit(30)
  114.             end
  115.             
  116.         call delete(fModifyMe||".tobedeleted")
  117.         say "Recovery of original file " || fModifyMe || " was successful."
  118.         end
  119.     exit(30)